package com.aifast.system.log.service;

import cn.hutool.core.lang.Console;
import com.aifast.common.base.Kv;
import com.aifast.common.forUpdate.ForUpdate;
import com.aifast.common.forUpdate.IForUpdateService;
import com.aifast.system.log.dao.ForUpdateDao;
import com.aifast.system.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ForUpdateService implements IForUpdateService {

    @Resource
    private ForUpdateDao dao;

    @Override
    public Kv<String,String> getAllTableNames() {
        List<Kv<String, String>> tableNames = dao.getAllTableNames();
        Kv<String,String> kv = new Kv<>();
        tableNames.forEach(i->{
            kv.put(i.get("name"),i.get("comment"));
        });
        return kv;
    }

    @Override
    public Kv<String,String> getTableColumnComment(String tableName) {
        List<Kv<String, String>> list = dao.getTableColumnComment(tableName);
        Kv<String,String> kv = new Kv<>();
        list.forEach(i->{
            kv.put(i.get("name"),i.get("comment"));
        });
        return kv;
    }

    @Override
    public void log(String toString) {
        log.info(toString);
    }

    @Override
    public Object getOperatorId() {
        return UserUtil.get().getUserId();
    }

    @Override
    public String getOperator() {
        return UserUtil.get().getRealName();
    }

    @Override
    public boolean save(String tableName ,Object dataId ,String content) {
        ForUpdate build = ForUpdate.builder().tableName(tableName)
                .tableDataId(dataId).content(content).userId(UserUtil.get().getUserId())
                .createTime(LocalDateTime.now().toString()).orgId(UserUtil.get().getOrgId()).build();
        return dao.insert(build) > 0;
    }
}
